using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;

namespace BlockOS.Client
{
    public class OptionsWindow : BaseInputActionMapWindow
    {
        [SerializeField]
        private TMP_InputField nameInput;
        [SerializeField]
        private Slider leftOffsetSilder;
        [SerializeField]
        private Slider rightOffsetSilder;
        [SerializeField]
        private Toggle[] controlModeToggles;

        protected override void Awake()
        {
            inputs["Back"].performed += Close;
        }

        public override void OnShow()
        {
            leftOffsetSilder.value = manager.offsetLeft;
            rightOffsetSilder.value = manager.offsetRight;
            nameInput.text = Settings.PLAYER_NAME;

            leftOffsetSilder.onValueChanged.AddListener(SetWindowLeftOffset);
            rightOffsetSilder.onValueChanged.AddListener(SetWindowRightOffset);

            for (int i = 0; i < controlModeToggles.Length; i++)
            {
                int t = i; // catch by clone, not ref.
                controlModeToggles[i].onValueChanged.AddListener(isOn =>
                {
                    if (isOn)
                    {
                        SetControlMode(t);
                    }
                });
            }
            try
            {
                controlModeToggles[(int)Settings.CONTROL_MODE].isOn = true;
            }
            catch
            {
                Debug.LogError($"{Settings.CONTROL_MODE}");
            }
        }

        public override void OnHide()
        {
            leftOffsetSilder.onValueChanged.RemoveListener(SetWindowLeftOffset);
            rightOffsetSilder.onValueChanged.RemoveListener(SetWindowRightOffset);

            foreach (var toggle in controlModeToggles)
            {
                toggle.onValueChanged.RemoveAllListeners();
            }

            Settings.SCREEN_OFFSET_LEFT = manager.offsetLeft;
            Settings.SCREEN_OFFSET_RIGHT = manager.offsetRight;
            Settings.PLAYER_NAME = nameInput.text;
        }

        protected override void OnDestroy()
        {
            inputs["Back"].performed -= Close;
        }

        private void Update()
        {
            if (Pointer.current.press.wasReleasedThisFrame)
            {
                HideGUIArea();
            }
        }

        private void Close(InputAction.CallbackContext obj)
        {
            manager.PopWindow(true);
        }

        public void ShowGUIArea()
        {
            manager.ShowArea();
        }

        public void HideGUIArea()
        {
            manager.HideArea();
        }

        public void SetControlMode(int mode)
        {
            Settings.CONTROL_MODE = (Settings.ControlMode)mode;
        }

        public void SetWindowLeftOffset(float offset)
        {
            manager.offsetLeft = offset;
            ShowGUIArea();
        }

        public void SetWindowRightOffset(float offset)
        {
            manager.offsetRight = offset;
            ShowGUIArea();
        }
    }
}
